//
//  FlowCurrencyController.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/11/12.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "FlowCurrencyController.h"
#import "ZXSegmentController.h"
#import "FlowCurrencyDetailController.h"
@interface FlowCurrencyController ()
@property (nonatomic,weak) ZXSegmentController* segmentController;
@end

@implementation FlowCurrencyController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = WhiteColor;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.font = [UIFont boldSystemFontOfSize:jisuan(32)];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = self.nameText.length > 0 ? self.nameText : @"趣豆明细";
    self.navigationItem.titleView = titleLabel;
    
    UIButton*btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, jisuan(18), jisuan(32));
    [btn setBackgroundImage:[UIImage imageNamed:@"nav_go_white"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"nav_go_white"] forState:UIControlStateSelected];
    [btn setBackgroundImage:[UIImage imageNamed:@"nav_go_white"] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(goBackAction)forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = back;
    
    [self initUI];
}

- (void)goBackAction {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -设置
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
    UIImage *image = [[UIImage alloc] init];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:image];
    self.navigationController.navigationBar.translucent = YES;
  
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
}

- (void)initUI {
    {
        
        UIView *headerView = [[UIView alloc] init];
        headerView.backgroundColor = WhiteColor;
        CGFloat topHeight = (IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? jisuan(430) : jisuan(408);
        headerView.frame = CGRectMake(0, 0, WIDTH, topHeight);
        [self.view addSubview:headerView];
        
        UIImageView *headerBG = [[UIImageView alloc] init];
        headerBG.image = [UIImage imageNamed:@"financialHeaderbg"];
        [headerView addSubview:headerBG];
        [headerBG mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(headerView.mas_top);
            make.centerX.mas_equalTo(headerView.mas_centerX);
            make.width.mas_equalTo(jisuan(750));
            CGFloat topHeight = (IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? jisuan(408) : jisuan(392);
            make.height.mas_equalTo(topHeight);
            
        }];
        UIImageView *numBgIV = [[UIImageView alloc] init];
        numBgIV.contentMode = UIViewContentModeScaleAspectFit;
        numBgIV.image = [UIImage imageNamed:@"financialTotolMoneyBG"];
        [headerView addSubview:numBgIV];
        [numBgIV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(headerBG.mas_bottom).with.offset(-jisuan(10));
            make.centerX.mas_equalTo(headerView.mas_centerX);
            make.width.mas_equalTo(jisuan(726));
            make.height.mas_equalTo(jisuan(214));
            
        }];
        
        UILabel *integralL = [[UILabel alloc] init];
        integralL.text = @"当前趣豆";
        integralL.textAlignment = NSTextAlignmentLeft;
        integralL.textColor = Color999999;
        integralL.font = Font(24);
        [headerView addSubview:integralL];
        [integralL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(numBgIV.mas_left).with.offset(jisuan(30));
            make.top.mas_equalTo(numBgIV.mas_top).with.offset(jisuan(30));
        }];
        
        UILabel *integralNumL = [[UILabel alloc] init];
        NSString *str = [NSString stringWithFormat:@"%@趣豆", [CommonUserInfo sharedInstance].bean_quota];
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attributeStr addAttribute:NSFontAttributeName value:Font(60) range:NSMakeRange(0, str.length - 1)];
        [attributeStr addAttribute:NSFontAttributeName value:Font(24) range:NSMakeRange(str.length - 2, 2)];
        integralNumL.attributedText = attributeStr;
        
        integralNumL.textAlignment = NSTextAlignmentLeft;
        integralNumL.textColor = Color1f89ff;
        [integralNumL sizeToFit];
        [headerView addSubview:integralNumL];
        [integralNumL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(numBgIV.mas_left).with.offset(jisuan(30));
            make.bottom.mas_equalTo(numBgIV.mas_bottom).with.offset(-jisuan(40));
        }];
        
        UIView *integralRuleView = [[UIView alloc] init];
        
        UITapGestureRecognizer *ruleViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ruleFlowGestureClick:)];
        [integralRuleView addGestureRecognizer:ruleViewGesture];
        [headerView addSubview:integralRuleView];
        [integralRuleView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(numBgIV.mas_right).with.offset(-jisuan(30));
            make.top.mas_equalTo(numBgIV.mas_top).with.offset(jisuan(30));
            make.height.mas_equalTo(jisuan(34));
            make.width.mas_equalTo(jisuan(160));
        }];
        
        UILabel *integralRuleL = [[UILabel alloc] init];
        integralRuleL.text = @"趣豆规则";
        integralRuleL.font = Font(24);
        integralRuleL.textColor = Color1f89ff;
        integralRuleL.textAlignment = NSTextAlignmentRight;
        [integralRuleView addSubview:integralRuleL];
        [integralRuleL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(integralRuleView.mas_right);
            make.centerY.mas_equalTo(integralRuleView.mas_centerY);
        }];
        
        UIImageView *questionIV = [[UIImageView alloc] init];
        questionIV.contentMode = UIViewContentModeScaleAspectFit;
        questionIV.image = [UIImage imageNamed:@"integralRecordRuleBG"];
        [integralRuleView addSubview:questionIV];
        [questionIV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.mas_equalTo(integralRuleL.mas_left).with.offset(-jisuan(10));
            make.centerY.mas_equalTo(integralRuleView.mas_centerY);
        }];
        
        FlowCurrencyDetailController *detailOneCtr = [[FlowCurrencyDetailController alloc] init];
        detailOneCtr.indexType = 1;
         FlowCurrencyDetailController *detailTwoCtr = [[FlowCurrencyDetailController alloc] init];
        detailTwoCtr.indexType = 2;
        NSArray* names = @[@"获得趣豆",@"使用趣豆"];
        NSArray* controllers = @[detailOneCtr,detailTwoCtr];

        ZXSegmentController* segmentController = [[ZXSegmentController alloc] initWithControllers:controllers
                                                                                   withTitleNames:names
                                                                                 withDefaultIndex:0
                                                                                   withTitleColor:[UIColor colorWithHexString:@"#333333"]
                                                                           withTitleSelectedColor:[UIColor blackColor]
                                                                                  withSliderColor:[UIColor redColor]];
        [self addChildViewController:(_segmentController = segmentController)];
        [self.view addSubview:segmentController.view];
        [segmentController didMoveToParentViewController:self];
        [self createAutolayout];
        
    }
}

- (void)createAutolayout{
    /*
     高度自由化的布局，可以根据需求，把segmentController布局成你需要的样子.(面对不同的场景，设置不同的top距离)
     */
    [_segmentController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(jisuan(408));
        make.left.right.bottom.mas_equalTo(0);
    }];
}

- (void)ruleFlowGestureClick:(UIButton *)button {
    
    [PPNetworkHelper POST:@"user/Bean/llbguize" parameters:nil success:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 1) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"data"] subBottonTitle:@"确定" handler:^(AlertViewClickBottonType bottonType) {
                
            }];
        }
    } failure:^(NSError *error) {
    }];
    
    
}

@end
